package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Skull;
import com.itgaoshu.hospitalproject.service.SkullService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("skull")
public class SkullController {

    @Autowired
    private SkullService skullService;

    //查询经办人列表的方法
    @RequestMapping("findAllSkull")
    @ResponseBody
    public Object findAllSkull(String skullName,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Skull> skulls = skullService.findAllSkull(skullName);
        PageInfo pageInfo = new PageInfo(skulls);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("msg","");
        tableData.put("code",0);
        tableData.put("count",pageInfo.getTotal());
        tableData.put("data",pageInfo.getList());
        return tableData;
    }


    //添加经办人的方法
    @ResponseBody
    @RequestMapping("addSkull")
    public Object addSkull(Skull skull){
        int count = skullService.count(skull);
        if(count==0){
            int result = skullService.addSkull(skull);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "这个经办人已经存在了";
        }
    }

    //删除经办人的方法
    @RequestMapping("deleteSkull")
    @ResponseBody
    public Object deleteSkull(Integer skullId){
        int result = skullService.deleteSkull(skullId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @ResponseBody
    @RequestMapping("editSkull")
    public Object editSkull(Skull skull){
        int result = skullService.editSkull(skull);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }


}
